home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
dev
/
c
/
AmiVoGL_MDEV.lha
/
hershey
/
src
/
htext.c.bak
< prev
next >
Wrap
Text File
|
1997-08-17
|
14KB
|
749 lines
/*
* This file is directly from the VOGLE library. It's simply had a few
* gratuitous name changes and some comments added.
*/
#ifdef SGI
#include <gl.h>
#include <device.h>
#else
#include "vogl.h"
#include "vodevice.h"
#endif
#include <stdio.h>
// #include <fcntl.h>
#include <stdlib.h>
#include "hershey.h"
#ifdef TC
extern double sin();
extern double cos();
#else
#include <math.h>
#endif
#include <string.h>
#ifndef PATH_SIZE
#define PATH_SIZE 256
#endif
#ifndef FONTLIB
# ifdef PC
# define FONTLIB "c:\\lib\\hershey\\"
# else
# ifdef AMIGA
# define FONTLIB "hershey:"
# else
# define FONTLIB "/usr/local/lib/hershey/"
# endif
# endif
#endif
#define ABS(a) (((a) < 0)? -(a) : (a))
#define MAX(a, b) (((a) < (b))? (b) : (a))
#define XCOORD(x) ((int)((x) - 'R'))
#define YCOORD(y) ((int)('R' - (y)))
#ifndef PI
#define PI 3.14159265358979
#endif
#ifndef D2R
#define D2R (PI / 180.0)
#endif
#define LEFT 0 /* The default */
#define CENTERED 1
#define RIGHT 2
static float tcos = 1.0, tsin = 0.0; /* For rotations */
static float SCSIZEX = 1.0, SCSIZEY = 1.0; /* Scale factors */
static int Justify = LEFT;
static int Fixedwidth = 0; /* Some flags */
static short nchars; /* No. in font */
int hLoaded = 0;
#ifdef AZTEC_C
#define __PROTOTYPE__
#endif
#ifdef __PROTOTYPE__
static int hershfont(char *); /* htext.c */
static int istrlength(); /* htext.c */
#else
static hershfont();
static int istrlength();
#endif
static char old_font[PATH_SIZE] = ""; /* So we don't have to reload it */
static char fpath[PATH_SIZE] = "";
static struct{
int as; /* Max ascender of a character in this font */
int dec; /* Max decender of a character in this font */
int mw; /* Max width of a character in this font */
char *p; /* All the vectors in the font */
char **ind; /* Pointers to where the chars start in p */
} ftab;
/* ---------------------------------------------------------------------
* Prototypes:
*/
#ifdef __PROTOTYPE__
static int hershfont(char *); /* htext.c */
static int istrlength(char *); /* htext.c */
#else /* __PROTOTYPE__ */
static int hershfont(); /* htext.c */
static int istrlength(); /* htext.c */
#endif /* __PROTOTYPE__ */
/* --------------------------------------------------------------------- */
/*
* hfont
* loads in a hershey font.
*/
void hfont(char *name)
{
/*
* check we aren't loading the same font twice in a row
*/
if (*name == '/') {
if (strcmp(strrchr(name, '/') + 1, old_font) == 0) {
return;
}
}
else if (strcmp(name, old_font) == 0) {
return;
}
/*
* Try and load it
*/
if (!hershfont(name)) {
fprintf(stderr, "hershlib: problem reading font file '%s'.\n", name);
gexit();
exit(1);
}
/*
* Save the name of it...
*/
if (*name == '/') strcpy(old_font, strrchr(name, '/') + 1);
else strcpy(old_font, name);
}
/* ------------------------------------------------------------------------ */
/*
* hnumchars
*
* Return the number of characters in the currently loaded hershey font.
*/
int hnumchars(void)
{
check_loaded("hnumchars");
return (int) nchars;
}
/* ------------------------------------------------------------------------ */
/*
* hsetpath
*
* Set the path of the directory to look for fonts.
*/
void hsetpath(char *path)
{
int l;
strcpy(fpath, path);
l = strlen(fpath);
#ifdef PC
if (fpath[l] != '\\') strcat(fpath, "\\");
#else
# ifdef AMIGA
if (fpath[l] != '/' && fpath[l] != ':') strcat(fpath, "/");
# else
if (fpath[l] != '/') strcat(fpath, "/");
# endif
#endif
}
/* ------------------------------------------------------------------------ */
/*
* hershfont
*
* Load in a hershey font. First try the environment, then the font library,
* if that fails try the current directory, otherwise return 0.
*/
static int hershfont(char *fontname)
{
FILE *fp;
int i;
int j;
short nvects;
short n;
char *flib;
char path[PATH_SIZE];
if (fpath[0] != '\0') {
strcpy(path, fpath);
strcat(path, fontname);
}
else if ((flib = getenv("HFONTLIB")) != (char *)NULL) {
strcpy(path, flib);
#ifdef PC
strcat(path, "\\");
#else
# ifdef AMIGA
if(path[strlen(path) - 1] != ':') strcat(path,"/");
# else
strcat(path, "/");
#endif
#endif
strcat(path, fontname);
}
else if ((flib = getenv("VFONTLIB")) != (char *)NULL) {
strcpy(path, flib); /* To be compatible with VOGLE */
#ifdef PC
strcat(path, "\\");
#else
# ifdef AZTEC_C
if(path[strlen(path) - 1] != ':') strcat(path,"/");
# else
strcat(path, "/");
# endif
#endif
strcat(path, fontname);
}
else {
strcpy(path, FONTLIB);
#ifdef PC
strcat(path, "\\");
#else
# ifdef AMIGA
if(path[strlen(path) - 1] != ':') strcat(path,"/");
# else
strcat(path, "/");
# endif
#endif
strcat(path, fontname);
}
#ifdef PC
if ((fp = fopen(path, "r+b")) == (FILE *)NULL &&
(fp = fopen(fontname, "r+b")) == (FILE *)NULL)
#else
# ifdef AMIGA
if ((fp= fopen(path, "rb")) == (FILE *) NULL &&
(fp= fopen(fontname, "rb")) == (FILE *) NULL)
# else
if ((fp= fopen(path, "r")) == (FILE *) NULL &&
(fp= fopen(fontname, "r")) == (FILE *) NULL)
# endif
#endif
{
fprintf(stderr, "hershlib: Can't open Hershey fontfile '%s' or './%s'.\n",
path,
fontname);
exit(1);
}
if (fread(&nchars, sizeof(nchars), 1, fp) != 1) {
return 0;
}
if (fread(&nvects, sizeof(nvects), 1, fp) != 1) {
return 0;
}
if (fread(&n, sizeof(n), 1, fp) != 1) {
return 0;
}
ftab.as = (int)n;
if (fread(&n, sizeof(n), 1, fp) != 1) {
return 0;
}
ftab.dec = (int)n;
if (fread(&n, sizeof(n), 1, fp) != 1) {
return 0;
}
ftab.mw = (int)n;
/*
* Allocate space for it all....
*/
if (hLoaded) {
if (ftab.ind[0]) free(ftab.ind[0]);
if (ftab.ind) free(ftab.ind);
hLoaded = 0;
}
ftab.ind = (char **)hallocate(sizeof(char *)*(nchars + 1));
ftab.p = (char *)hallocate((unsigned)(2 * nvects));
/*
* As we read in each character, figure out what ind should be
*/
for (i = 0; i < nchars; i++) {
if (fread(&n , sizeof(n), 1, fp) != 1) {
return 0;
}
if (fread(ftab.p, 1, (unsigned)n, fp) != (unsigned)n) {
return 0;
}
ftab.ind[i] = ftab.p;
ftab.p += n;
}
ftab.ind[nchars] = ftab.p; /* To Terminate the last one */
fclose(fp);
hLoaded = 1;
return 1;
}
/* ------------------------------------------------------------------------ */
/*
* hgetcharsize
*
* get the width and height of a single character. At the moment, for
* the hershey characters, the height returned is always that of the
* difference between the maximun descender and ascender.
*
*/
void hgetcharsize(char c, float *width, float *height)
{
check_loaded("hgetcharsize");
*height = (float)(ftab.as - ftab.dec) * SCSIZEY;
if (Fixedwidth)
*width = (float)ftab.mw * SCSIZEX;
else
*width = (float)(ftab.ind[c - 32][1] - ftab.ind[c - 32][0]) * SCSIZEX;
}
/* ------------------------------------------------------------------------ */
/*
* hdrawchar
*
* Display a character from the currently loaded font.
*/
void hdrawchar(int c)
{
char *p, *e;
int Move, i, x, y, xt, yt;
float xs, ys, xp, yp, tmp, xtmp, ytmp;
check_loaded("hdrawchar");
if ((i = c - 32) < 0)
i = 0;
if (i >= nchars)
i = nchars - 1;
Move = 1;
xt = yt = 0;
if (Justify == LEFT) {
xt = (Fixedwidth ? -ftab.mw / 2 : XCOORD(ftab.ind[i][0]));
yt = ftab.dec;
}
else if (Justify == RIGHT) {
xt = (Fixedwidth ? ftab.mw / 2 : -XCOORD(ftab.ind[i][0]));
yt = ftab.dec;
}
e = ftab.ind[i + 1];
p = ftab.ind[i] + 2;
xtmp = ytmp = 0.0;
while(p < e) {
x = XCOORD((int)*p++);
y = YCOORD((int)*p++);
if (x != -50) { /* means move */
xp = (float)(x - xt) * SCSIZEX;
yp = (float)(y - yt) * SCSIZEY;
tmp = xp;
xp = tcos*tmp - tsin*yp;
yp = tsin*tmp + tcos*yp;
xs = xp - xtmp;
ys = yp - ytmp;
xtmp = xp;
ytmp = yp;
if (Move) {
Move = 0;
rmv((Coord)xs, (Coord)ys, (Coord)0.0);
}
else
rdr((Coord)xs, (Coord)ys, (Coord)0.0);
}
else {
Move = 1;
}
}
/*
* Move to right hand of character.
BLARK
*/
tmp = Fixedwidth ? (float)ftab.mw : (float)(ftab.ind[i